﻿using System;
using StatisticsCalculator;
using System.Text;

namespace Presentation
{
    public class Presenter
    {
        private IView view;
        private readonly ILogger textLogger;
        private Statistics statistics;
        public Presenter(IView mView, ILogger textLogger){
            view = mView;
            view.OnInputData = InputData;
            this.textLogger = textLogger;
            setupView(view);
        }

        public void setupView(IView mView)
        {
            view.OnProcessExpectedValueOfUniformDistribution = ProcessExpectedValueUniformDistribution;
            view.OnProcessVarianceOfUniformDistribution = ProcessVarianceUniformDistribution;
            view.OnProcessSampleMean = ProcessSampleMean;
            view.OnProcessSampleVariance = ProcessSampleVariance;
            view.OnProcessSampleSkewness = ProcessSampleSkewness;
            view.OnProcessSampleKurtosis = ProcessSampleKurtosis;
            view.OnClearEntries = ProcessClearEntries;
            view.OnShowLogs = ProcessShowLogs;
        }
        public void InputData()
        {
            try
            {
                if (view.SampleData == null)
                {
                    throw new ArgumentException("Empty input data.");
                }
                if (string.IsNullOrEmpty(view.SampleData))
                {
                    throw new ArgumentException("Invalid input data.");
                }
                statistics = new Statistics(processInputString(view.SampleData));
                view.ErrorMessage = String.Empty;
                textLogger.AddLog("Entered this sample data: " + view.SampleData);
            }
            catch (Exception e)
            {
                view.ErrorMessage = e.Message;
                return;
            }
          
        }
        public void ProcessExpectedValueUniformDistribution()
        {
            try
            {
                double result = statistics.calculateExpectedValueForUniformDistribution();
                view.ExpectedValueUniformDistribution = Convert.ToString(Math.Round(result, 5));
                textLogger.AddLog("Result Expected Value " + view.ExpectedValueUniformDistribution);
            }
            catch (Exception e)
            {
                view.ErrorMessage = e.Message;
            }
        }
        public void ProcessVarianceUniformDistribution()
        {
            try
            {
                double result = statistics.calculateVarianceForUniformDistribution();
                view.VarianceUniformDistribution = Convert.ToString(Math.Round(result, 5));
                textLogger.AddLog("Result Variance for distrubution " + view.VarianceUniformDistribution);
            }
            catch (Exception e)
            {
                view.ErrorMessage = e.Message;
            }
        }
        public void ProcessSampleMean()
        {
            try
            {
                double result = statistics.calculateMean();
                view.SampleMean = Convert.ToString(Math.Round(result, 5));
                textLogger.AddLog("Result of Sample Mean " + view.VarianceUniformDistribution);
            }
            catch (Exception e)
            {
               view.ErrorMessage =  e.Message;
            }
        }
        public void ProcessSampleVariance()
        {
            try
            {
                double result = statistics.calculateVariance();
                view.SampleVariance = Convert.ToString(Math.Round(result, 5));
                textLogger.AddLog("Result of Sample Variance " + view.SampleVariance);
            }
            catch (Exception e)
            {
                view.ErrorMessage = e.Message;
            }
        }
        public void ProcessSampleSkewness()
        {
            try
            {
                double result = statistics.calculateSkewness();
                view.SampleSkewness = Convert.ToString(Math.Round(result, 5));
                textLogger.AddLog("Result of Sample Skewness " + view.SampleSkewness);
            }
            catch (Exception e)
            {
                view.ErrorMessage = e.Message;
            }
        }
        public void ProcessSampleKurtosis()
        {
            try
            {
                double result = statistics.calculateKurtosis();
                view.SampleKurtosis = Convert.ToString(Math.Round(result, 5));
                textLogger.AddLog("Result of Sample Kurtosis " + view.SampleKurtosis);
            }
            catch (Exception e)
            {
                view.ErrorMessage = e.Message;
            }
        }
        private void ProcessShowLogs()
        {
            try
            {
                view.Logs = textLogger.GetLogs();
            }
            catch (Exception)
            {
                view.Logs = new string[] { };
            }
        }
        public void ProcessClearEntries()
        {
            view.SampleData = String.Empty;
            view.ExpectedValueUniformDistribution = String.Empty;
            view.VarianceUniformDistribution = String.Empty;
            view.SampleMean = String.Empty;
            view.SampleVariance = String.Empty;
            view.SampleSkewness = String.Empty;
            view.SampleKurtosis = String.Empty;
            view.ErrorMessage = String.Empty;

    
        }
        public static double[] processInputString(string inputString)
        {
            char[] delimeters = { ',', ' ' };
            double[] data = Array.ConvertAll(inputString.Split(delimeters), Double.Parse);
            return data;
        }
    }

}